﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Collections;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Remoting.Channels.Tcp;
using System.ServiceModel;

namespace Mongaba.Core.Registers
{
    internal class BindingCreator
    {
        #region Process Binding
        
       
        public static bool IsProcessChannelCreated
        {
            get
            {
                return  processBinding != null;
            }
        }

        public static void CreateProcessBinding()
        {
           
                processBinding = new NetTcpBinding();
                configure(processBinding, Utilities.ProcessBindingName);                
            
        }

        public static NetTcpBinding GetProcessBinding()
        {
            CreateProcessBinding();
            return processBinding;
        }
        static NetTcpBinding processBinding;
            
        #endregion

        #region Coordinator Binding

        public static void CreateCoordinatorBinding()
        {
            
                coordinatorBinding = new NetTcpBinding();
                configure(coordinatorBinding, Utilities.CoordinatorBindingName);
            
        }
        
        static void configure(NetTcpBinding ntcp, string name)
        {
            ntcp.OpenTimeout = TimeSpan.FromSeconds(10);
            ntcp.CloseTimeout = TimeSpan.FromSeconds(10);
            //ntcp.SendTimeout = TimeSpan.FromSeconds(10);
            //ntcp.ReceiveTimeout = TimeSpan.FromSeconds(10); 
            ntcp.TransferMode = TransferMode.Buffered;
            ntcp.Security.Mode = SecurityMode.None;
            ntcp.ReaderQuotas.MaxArrayLength = 128000;
            ntcp.ReaderQuotas.MaxBytesPerRead = 128000;
            ntcp.Name = name;
            ntcp.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None;
            ntcp.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;            
        }
        public static NetTcpBinding GetCoordinatorBinding()
        {
            
                CreateCoordinatorBinding();
            
            return coordinatorBinding;
        }


        static NetTcpBinding coordinatorBinding;


        public static bool IsCoordinatorChannelRegistered
        {
            get
            {
                return coordinatorBinding != null;
            }
        }

        public static NetTcpBinding CreateNewTcpBinding(string name)
        {
            NetTcpBinding ntcp = new NetTcpBinding();
            configure(ntcp, name);
            return ntcp;        
        }

        #endregion
        
    }
}
